
<template>
  <div class="app-container">
    <el-tabs v-model="activeName">
      <el-tab-pane label="角色管理" name="first">
        <el-button type="primary" @click="isClose=true">新增角色</el-button>
        <el-card class="box-card">
          <el-table
            v-loading="loading"
            style="width: 100%"
            :data="list"
          >
            <el-table-column
              label="序号"
              width="80"
              :index="indexMethod"
            />
            <el-table-column
              prop="name"
              label="角色"
              width="280"
            />
            <el-table-column
              prop="description"
              label="描述"
            />
            <el-table-column
              fixed="right"
              label="操作"
              width="320"
            >
              <template slot-scope="scope">
                <el-button size="small" type="success">分配权限</el-button>
                <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="delFn(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row type="flex" justify="end" align="middle" style="height:60px">
            <el-pagination
              :current-page.sync="page.page"
              background
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              :page-sizes="[5, 10, 15, 20]"
              :page-size.sync="page.pagesize"
              @size-change="getRoleListFn"
              @current-change="getRoleListFn"
            >
              />
            </el-pagination></el-row>
        </el-card>
      </el-tab-pane>
      <el-tab-pane label="公司信息" name="second">
        <el-alert
          title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
          type="info"
          show-icon
          :closable="false"
        />
        <el-form label-width="120px" style="margin-top:50px">
          <el-form-item label="公司名称">
            <el-input v-model="formData.name" disabled style="width:400px" />
          </el-form-item>
          <el-form-item label="公司地址">
            <el-input v-model="formData.companyAddress" disabled style="width:400px" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="formData.mailbox" disabled style="width:400px" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <roleDialog ref="roleDialog" :dialog-form-visible.sync="isClose" />
  </div>
</template>

<script>
import { getRoleListApi, deleteRoleApi, getCompanyInfoApi } from '@/api/setting'
import roleDialog from '@/views/setting/roleDialog.vue'
import { mapGetters } from 'vuex'
export default {
  name: 'SettingIndex',
  components: { roleDialog },
  data() {
    return {
      activeName: 'first',
      page: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      list: [],
      loading: false,
      isClose: false,
      formData: {}
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  watch: {
    activeName(val) {
      if (val === 'second' && !this.formData.id) {
        this.getCompanyInfoFn()
      }
    }
  },
  created() {
    this.getRoleListFn()
  },
  methods: {
    indexMethod() {
      return this.index * 1
    },
    async getRoleListFn() {
      try {
        this.loading = true
        const { total, rows } = await getRoleListApi(this.page)
        this.total = total
        this.list = rows
        if (this.total > 0 && this.list.length === 0) {
          this.page.page--
          this.getRoleListFn()
        }
      } catch (err) {
        console.log(err)
      } finally {
        this.loading = false
      }
    },
    edit(row) {
      this.isClose = true
      this.$refs.roleDialog.roleDialogForm = { ...row }
    },
    async delFn(id) {
      //  提示
      try {
        await this.$confirm('确认删除该角色吗', '删除提示', {
          cancelButtonText: '取消',
          confirmButtonText: '确定'
        })
        // 只有点击了确定 才能进入到下方
        await deleteRoleApi(id) // 调用删除接口
        this.getRoleListFn() // 重新加载数据
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    async  getCompanyInfoFn() {
      const res = await getCompanyInfoApi(this.companyId)
      console.log(res)
      this.formData = res
    }
  }
}

</script>

<style lang="scss" scoped>

</style>

